# -*- coding: cp866 -*-

import datetime
import time

MS_MSGID_TO_CONTACTID = {
    '13':'1103',
    '22':'1101'
}
MS_MSGID_TO_CONTACTID_INPUT1 = {
    '11':'1103',
    '13':'1103',
    '20':'1101',
    '22':'1101'
}
MS_BOLID_TO_CONTACTID = {
    'ВЗЛОМКОРПУСА':'3831',
    'ВОССТ.КОРПУСА':'3833',
    'ПОЖ':'1101',
    'ПОЖАР':'1101',
    'СБРОСТРЕВОГИШС':'1103',
    'ВНИМАНИЕ':'1181',
    'ТРЕВОГА':'1231',
	'ТИХАЯТРЕВОГА':'1221',
	'ТРЕВОГАВХОДА':'1341',
	'ИЗМ.ДАТЫ':'6253',
	'ВЗЯТШС':'4003',
	'СНЯТШС':'4011',
	'ПРОГРАММИРОВАНИЕ':'6271',
	'СБРОСПРИБОРА':'3051',
	'ОБР':'3311',
	'КЗ':'3321',
	'АВПИТ':'3011',
	'ВОССТ':'3013',
	'ОБРЫВШС':'3311',
	'РАЗДЕЛВЗЯТ':'4023',
	'РАЗДЕЛСНЯТ':'4021',
	'КОРОТКОЕЗАМЫКАН':'3321',
	'ИДЕНТИФИКАЦИЯХО':'4221',
	'КЗВЫХОДА':'3201',
	'ОБРЫВВЫХОДА':'3201',
	'НЕИСПР.ТЕЛ.ЛИНИИ':'3501',
	'НЕИЗ.СОБЫТИЕ091':'3503',
	'ВЫКЛ.ПРИНТЕРА':'3361',
	'ВКЛ.ПРИНТЕРА':'3363',
	'ИЗМ.ВРЕМЕНИ':'6253'
}



class Event:
    def __init__(self):
        self.eventId    = 0
        self.objectId   = 0
        self.eventReceiveTime   = datetime.datetime.fromtimestamp(time.time())
        self.eventGenerateTime  = self.eventReceiveTime

        self.isWrittenToPCNMySQL        = False
        self.isWrittenToPCNFB           = False
        self.isWrittenToCOMSecurithor   = False
        self.isWrittenToWEBUIStatic     = False

        self.pgsmType       = 0

        self.contactIdOrig  = ''
        self.contactIdFull  = self.contactIdOrig
        self.contactIdType  = ''
        self.contactIdCode  = ''
        self.contactIdZona  = ''
        self.contactIdGroup = ''

        self.gpsLatitude    = 0.0
        self.gpsLongitude   = 0.0
        self.gpsSpeed       = 0.0

    def __cmp__(self, y):
        if y == None:
            return 1
        if self.eventGenerateTime > y.eventGenerateTime:
            return 1
        if self.eventGenerateTime < y.eventGenerateTime:
            return -1
        if self.eventReceiveTime > y.eventReceiveTime:
            return 1
        if self.eventReceiveTime < y.eventReceiveTime:
            return -1
        if self.eventId > y.eventId:
            return 1
        if self.eventId < y.eventId:
            return -1
        return 0


    def fromPGSM(self, rawEvent):
        global MS_MSGID_TO_CONTACTID
        global TIME_FORMAT_MAGIC
        self.hasStatic = True
        self.contactIdType = MS_MSGID_TO_CONTACTID.get(str(rawEvent['type']), None)
        if self.contactIdType == None:
            raise Exception('unknown PGSM type %s' % (str(rawEvent['type'])))
        self.eventGenerateTime = datetime.strptime(rawEvent['datetime'], TIME_FORMAT_MAGIC)
        self.contactIdZona = '099'
        #self.eventId = rawEvent['eventInternalId']
    def fromPGSM_INPUT1(self, rawEvent):
        global MS_MSGID_TO_CONTACTID
        global TIME_FORMAT_MAGIC
        self.hasStatic = True
        self.contactIdType = MS_MSGID_TO_CONTACTID_INPUT1.get(str(rawEvent['type']), None)
        if self.contactIdType == None:
            raise Exception('unknown PGSM type %s' % (str(rawEvent['type'])))
        self.eventGenerateTime = datetime.strptime(rawEvent['datetime'], TIME_FORMAT_MAGIC)
        self.contactIdZona = '098'
        #self.eventId = rawEvent['eventInternalId']


